class ContactsController < ApplicationController
  def new
    @contact = Contact.new
    @driver = Driver.find(params[:driver_id])
  end
  
  def create
    @driver = Driver.find(params[:driver_id])
    respond_to do |format|
      if @driver.contacts.create(params[:contact])
        format.html  { redirect_to(contacts_driver_path(@driver),
                      :notice => 'Contact was added successfully for the driver.') }
        format.json  { render :json => @driver,
                      :status => :created, :location => @driver }
      else
        format.html  { render :action => "new" }
        format.json  { render :json => @driver.errors,
                      :status => :unprocessable_entity }
      end
    end
  end
  
  def edit
    @contact = Contact.find(params[:id])
    @driver = Driver.find(params[:driver_id])
  end
  
  def update
    @driver = Driver.find(params[:driver_id])
    @contact = Contact.find(params[:id])
    respond_to do |format|
      if @contact.update_attributes(params[:contact])
        format.html  { redirect_to(contacts_driver_path(@driver),
                      :notice => 'Contact was updated successfully for the driver.') }
        format.json  { render :json => @driver,
                      :status => :created, :location => @driver }
      else
        format.html  { render :action => "new" }
        format.json  { render :json => @driver.errors,
                      :status => :unprocessable_entity }
      end
    end
  end
  
  def destroy
    @driver = Driver.find(params[:driver_id])
    @address = Address.find(params[:id])
    respond_to do |format|
      if Driver.delete_contact(params[:driver_id], params[:id])
        format.html  { redirect_to(contacts_driver_path(@driver),
                      :notice => 'Contact was deleted successfully for the driver.') }
        format.json  { render :json => @driver,
                      :status => :created, :location => @driver }
      else
        format.html  { render :action => "new" }
        format.json  { render :json => @driver.errors,
                      :status => :unprocessable_entity }
      end
    end
  end
end